//
//  DAMessageDetailBorrowBookTimeLineView.m
//  DAKnowledge
//
//  Created by DreamArts on 16/7/8.
//  Copyright © 2016年 lidongyang. All rights reserved.
//

#import "DAMessageDetailBorrowBookTimeLineView.h"
#import "Masonry.h"
#import "DALabel.h"
#import "DARateView.h"
#import "DAHeader.h"
#import "Utility.h"
#import "DAMessageDetailModel.h"
#import "UIImageView+WebCache.h"

@interface DAMessageDetailBorrowBookTimeLineView (){
    UIImageView *userHeaderImageView;    // 头像
    DALabel *userNameLabel;              // 用户名
    UILabel *timeline;                   // 时间轴
    UIView *statusBackView;              // 时间轴状态的backView
    UIImageView *statusImageView;        // 时间轴状态的 imageview
    DALabel *timeLabel;                  // 时间
    UIImageView *bookImageView;          // 图书的图片
    DALabel *bookNameLabel;              // 书名
    DALabel *borrowTimeLabel;            // 借阅期
    DALabel *bookSourceLabel;            // 持有人
    UIImageView *commentImageView;       // 右边评论的按钮图片
    UILabel *bottomLine;                 // 底部线的label
    UIView *commentBackView;             // 评论按钮 点击的view (comment太小 不好点击)
}

@end
@implementation DAMessageDetailBorrowBookTimeLineView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor colorWithHexString:backGroundColor];
        // 头像
        userHeaderImageView = [[UIImageView alloc]init];
        userHeaderImageView.layer.cornerRadius = 5;
        [self addSubview:userHeaderImageView];
        
        UITapGestureRecognizer *userFaceTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickUserFace:)];
        userHeaderImageView.userInteractionEnabled = YES;
        [userHeaderImageView addGestureRecognizer:userFaceTap];
        
        // 用户名
        userNameLabel = [[DALabel alloc]init];
        userNameLabel.font = [UIFont systemFontOfSize:10];
        userNameLabel.textColor = [UIColor colorWithHexString:bookNameColor];
        [self addSubview:userNameLabel];
        // 时间轴
        timeline = [[UILabel alloc]init];
        timeline.backgroundColor = [UIColor colorWithHexString:timeLineColor];
        [self addSubview:timeline];
        // 时间轴状态的backView
        statusBackView = [[UIView alloc]init];
        statusBackView.backgroundColor = [UIColor supportGreenColor];
        statusBackView.layer.cornerRadius = 10;
        [self addSubview:statusBackView];
        // 状态图片
        statusImageView = [[UIImageView alloc]init];
        statusImageView.image = [UIImage imageNamed:@"book@3x"];
        [statusBackView addSubview:statusImageView];
        // 时间
        timeLabel = [[DALabel alloc]init];
        timeLabel.font = [UIFont systemFontOfSize:12];
        timeLabel.textColor = [UIColor colorWithHexString:timeColor];
        [self addSubview:timeLabel];
        // 评论按钮的图片
        commentImageView = [[UIImageView alloc]init];
        commentImageView.image = [UIImage imageNamed:@"reply@3x"];
        [self addSubview:commentImageView];
        // 盖在评论按钮图片的上view
        commentBackView = [[UIView alloc]init];
        [self addSubview:commentBackView];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickCommentImage:)];
        [commentBackView addGestureRecognizer:tap];
        // 图书image
        bookImageView = [[UIImageView alloc]init];
        bookImageView.layer.borderColor = [UIColor whiteColor].CGColor;
        bookImageView.layer.borderWidth = 3;
        [self addSubview:bookImageView];
        
        UITapGestureRecognizer *BookImagetap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickBookImage:)];
        bookImageView.userInteractionEnabled = YES;
        [bookImageView addGestureRecognizer:BookImagetap];
        
        // 图书名字
        bookNameLabel = [[DALabel alloc]init];
        bookNameLabel.font = [UIFont systemFontOfSize:13];
        [self addSubview:bookNameLabel];
        // 借阅期
        borrowTimeLabel = [[DALabel alloc]init];
        borrowTimeLabel.font = [UIFont systemFontOfSize:12];
        borrowTimeLabel.textColor = [UIColor colorWithHexString:borrowTimeColor];
        [self addSubview:borrowTimeLabel];
        // 持有人
        bookSourceLabel = [[DALabel alloc]init];
        bookSourceLabel.font = [UIFont systemFontOfSize:12];
        bookSourceLabel.verticalAlignment = VerticalAlignmentBottom;
        bookSourceLabel.textColor = [UIColor colorWithHexString:borrowTimeColor];
        [self addSubview:bookSourceLabel];
      
        // 底部的线
        bottomLine = [[UILabel alloc]init];
        bottomLine.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [self addSubview:bottomLine];
        
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    // 页面布局
    [userHeaderImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(8);
        make.left.mas_equalTo(5);
        make.width.height.mas_equalTo(34);
    }];
    [userNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(userHeaderImageView.mas_bottom).offset(3);
        make.left.mas_equalTo(userHeaderImageView);
        make.right.mas_equalTo(timeline.mas_left).offset(-2);
    }];
    [timeline mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.width.mas_equalTo(3);
        make.left.mas_equalTo(userHeaderImageView.mas_right).offset(20);
    }];
    
    [statusBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(15);
        make.left.mas_equalTo(userHeaderImageView.mas_right).offset(11);
        make.width.height.mas_equalTo(20);
    }];
    
    [statusImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(5);
        make.bottom.mas_equalTo(-5);
        make.left.mas_equalTo(3);
        make.right.mas_equalTo(-3);
    }];
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(15);
        make.left.mas_equalTo(statusBackView.mas_right).offset(8);
        make.right.mas_equalTo(-50);
    }];
    
    [commentImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(15);
        make.right.mas_equalTo(-8);
        make.width.mas_equalTo(18);
        make.height.mas_equalTo(12);
    }];
    [commentBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(5);
        make.left.mas_equalTo(timeLabel.mas_right).offset(3);
        make.right.mas_equalTo(-5);
        make.height.mas_equalTo(25);
    }];
    
    [bookImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(timeLabel.mas_bottom).offset(8);
        make.left.mas_equalTo(statusBackView.mas_right).offset(8);
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(95);
    }];
   // bookImageView.backgroundColor = [UIColor greenColor];
    
    [bookSourceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(bookImageView).offset(2);
        make.left.mas_equalTo(bookImageView.mas_right).offset(8);
        make.right.mas_equalTo(-50);
        // make.height.mas_equalTo(20);
    }];
    
    [borrowTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(bookSourceLabel.mas_top).offset(-5);
        make.left.mas_equalTo(bookSourceLabel);
        make.width.mas_equalTo(bookSourceLabel);
    }];
    
    [bookNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(bookImageView);
        make.left.mas_equalTo(bookSourceLabel);
        make.right.mas_equalTo(-20);
    }];
    
    [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-1);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(1);
    }];
    
}

- (void)setModel:(DAMessageDetailModel *)model{
    _model = model;
    // 头像
    [userHeaderImageView sd_setImageWithURL:[NSURL URLWithString:model.userFace] placeholderImage:[UIImage imageNamed:@"holderface"] options:SDWebImageRefreshCached];
    // 用户名
    [userNameLabel setText:model.userName fitToContent:YES];
    
    // 国际化设置
    NSString *back = NSLocalizedString(@"Borrow", nil);
    // 时间
    timeLabel.text = [NSString stringWithFormat:@"%@ %@", model.timelineDate, back];
    
    //    // 书的图片
    //    UIView *bookCover = [[UIView alloc]initWithFrame:CGRectMake(0 , 0, bookImageView.frame.size.width, bookImageView.frame.size.height)];
    //    bookCover.backgroundColor = [UIColor supportGreenColor];
    //    DALabel *bookCoverName = [[DALabel alloc]initWithFrame:CGRectMake(5, 5, bookCover.frame.size.width -10, bookCover.frame.size.height - 10)];
    //    [bookCoverName setText:model.bookName fitToContent:YES];
    //    bookCoverName.verticalAlignment = VerticalAlignmentMiddle;
    //    bookCoverName.textAlignment = NSTextAlignmentCenter;
    //    [bookCoverName setTextColor:[UIColor whiteColor]];
    //    bookCoverName.font = [UIFont systemFontOfSize:12];
    //    [bookCover addSubview:bookCoverName];
    //
    //    UIImage *image = [self makeImageWithView:bookCover];
    // 图书 图片
    [bookImageView sd_setImageWithURL:[NSURL URLWithString:model.bookImage] placeholderImage:nil];
    // 书名
    [bookNameLabel setText:model.bookName fitToContent:YES];
    // 借阅期
    // 国际化设置
    NSString *borrowTime = NSLocalizedString(@"borrowTime", nil);
    NSString *str = [NSString stringWithFormat:@"%@: %@", borrowTime,model.borrowDuration];
    [borrowTimeLabel setText:str fitToContent:YES];
    // 持有人
    NSString *bookSourceStr = NSLocalizedString(@"bookSource", nil);
    NSString *sourcStr = [NSString stringWithFormat:@"%@: %@", bookSourceStr,model.bookSource];
    [bookSourceLabel setText:sourcStr fitToContent:YES];
    
}

#pragma mark - 点击方法
- (void)clickCommentImage:(UITapGestureRecognizer *)tap{
    [self.delegate clickMessageDetailBorrowBookComment];
}

#pragma amrk - 点击图书图片的手势方法
- (void)clickBookImage:(UITapGestureRecognizer *)tap{
    [self.delegate clickMessageDetailBorrowImageWithID:self.model.userID];
}

#pragma mark - 点击头像的手势方法
- (void)clickUserFace:(UITapGestureRecognizer *)tap{
    [self.delegate clickMessageDetailBorrowUserFaceWithUserID:self.model.userID];
}





/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
